package edu.mis.modules.orderList;

import com.jfinal.core.Controller;

import edu.mis.model.OrderList;
import edu.mis.model.OrderStatus;
import edu.mis.model.RoomType;
import edu.mis.util.DateUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


public class OrderListController extends Controller {
	
	public static OrderListService service = new OrderListService();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	public void index() {
		render("orderList.html");
	}

	
	public void list(){
		setAttr("data", service.findAllOrderListWithStatus());
		renderJson();  
	}

	/**
	 * 添加订单视图
	 */
	public void add() {
		render("add.html");  //（默认对应add.html，否则需显性指定，如render("addOrderList.html")）
	}

	public void addOrderList(){
		OrderList orderList  =  getModel(OrderList.class);   //使用getModel方法获取表单值，表单中name应使用对象名.属性名，如orderList.id, orderList.orderListQty,详见jfinal手册
		service.addOrderList(orderList);

		renderJson(); 
	}


	public void checkinList(){
		setAttr("data", service.findCheckinOrderListWithCheckinAndStatus(simpleDateFormat.format(new Date()), 1));
		renderJson();
	}
	public void checkoutList(){
		setAttr("data", service.findCheckoutOrderListWithCheckoutAndStatus(simpleDateFormat.format(new Date()), 3));
		renderJson();
	}
	/**
	 * 删除订单
	 * 测试访问地址：localhost:8080/orderList/delete?id=1
	 */
	public void delete() {
		if(service.deleteOrderListById(getParaToInt("id"))){
			setAttr("result", true);
			setAttr("msg", "删除订单成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
	public void detail(){
		setAttr("data", service.findAllOrderDetailsById(getParaToInt("id")));
		renderJson();
	}
	public void detailCustomer(){
		setAttr("data", service.findAllOrderDetailCustomersById(getParaToInt("id")));
		renderJson();
	}
	/**
	 * 修改订单信息视图（默认对应edit.html，否则需显性指定，如render("editOrderListQty.html")，）
	 */
	public void edit() {
		setAttr("orderList", service.findOrderListById(getParaToInt("id")));  //此处orderList应与表单中${(orderList.name)!}等中的orderList保持一致。
	}
	public void todayCheckinList(){
		setAttr("data", service.findCheckinOrderListWithCheckinAndStatus(simpleDateFormat.format(new Date()), 3));
		renderJson();
	}
	public void todayCheckoutList(){
		setAttr("data", service.findCheckoutOrderListWithCheckoutAndStatus(simpleDateFormat.format(new Date()), 2));
		renderJson();
	}
	public void todayDetail(){
		setAttr("data", service.findAllOrderDetailsById(getParaToInt("id")));
		renderJson();
	}
    public void updateOrderList(){
        OrderList orderList  =  getModel(OrderList.class);
        OrderList prevOrderList = service.findOrderListById(orderList.getId());
        Integer delayDays = orderList.getDelayDays();
        Double totalPrice = (delayDays-prevOrderList.getDelayDays())* prevOrderList.getPricePerDay() + prevOrderList.getTotalPrice();
        Date newCheckout= DateUtil.newDelayDate(prevOrderList.getCheckout(), delayDays);
        if(service.updateOrderList(orderList.setTotalPrice(totalPrice).setNewCheckout(newCheckout))){
            setAttr("result", true);
            setAttr("msg", "修改退房日期成功!");
        }else{
            setAttr("result", false);
        }
        renderJson();
    }

	public void view_checkinList(){
		render("checkinList.html");
	}
	public void view_checkoutList(){
		render("checkoutList.html");
	}
    public void view_checkout(){
        setAttr("orderList", service.findOrderListById(getParaToInt("id")));
        render("checkout.html");
    }
	public void view_detail(){
		setAttr("orderList", service.findOrderListById(getParaToInt("id")));
		render("detail.html");
	}
	public void view_detailCustomer(){
		setAttr("orderList", service.findOrderListById(getParaToInt("id")));
		render("detailCustomer.html");
	}
	public void view_todayCheckinList(){
		render("todayCheckinList.html");
	}
    public void view_todayCheckoutList(){
        render("todayCheckoutList.html");
    }
	public void view_todayDetail(){
		setAttr("orderList", service.findOrderListById(getParaToInt("id")));
		render("todayDetail.html");
	}
}